<?php
class Index extends Public_Controller {

    const LIMIT_AUTOCOMPLETE = 10;
    
    public function __construct() {
        parent::__construct();
        $this->_load();
    }
    
    public function index() {
        $squery = trim($this->input->get('query'));
        $this->get_list($squery);
        $this->_replacePhrase();
    }
    
    public function ajax_get_auto_complete_data() {
        $skeyword = trim($this->input->get('term'));
        $this->load->model('tag', 'tag', false, 'tag');
        $adata = $this->tag->gets($skeyword, self::LIMIT_AUTOCOMPLETE);
        $ideltra_autocomplete = self::LIMIT_AUTOCOMPLETE - count($adata);
        
        if($ideltra_autocomplete){
            $adata += $this->product->get_by_search_keyword($skeyword, $ideltra_autocomplete);
        }
        
        //Sort array by name.
        if(sort($adata)){
            echo json_encode($adata);
            return true;
        }
        
        echo 'Can not sort array data';
    }

    private function _load() {
        $this->load->helper(array('url', 'form'));
        $this->load->model('product', 'product', false, 'product');
        $this->load->model('user', 'user', false, 'user');
    }

    private function _replacePhrase() {
        $this->lang->load('public/general', $this->_slang_name);
        $this->lang->load('public/product', $this->_slang_name);
        $this->_adata['list'] = $this->_list;
        $this->_adata['userLog'] = $this->_ouser;
        $this->_adata['security'] = (object) array('token_name' => $this->security->get_csrf_token_name(),
                    'token' => $this->security->get_csrf_hash());
        $this->fill_phrase('index', $this->_adata, $this->lang->language);
    }

    public function get_list($squery) {
        $aPager = array(
            'first_url' => site_url('search/index/index/?')  . http_build_query(array('query' => $squery)),
            'suffix' => '/?' . http_build_query(array('query' => $squery)),
            'per_page' => 10,
            'num_links' => 6,
            'cur_tag_open' => '<li><span><a class="normal-text">',
            'cur_tag_close' => '</a></span></li>',
            'num_tag_open' => '<li>',
            'num_tag_close' => '</li>',
            'use_page_numbers' => true,
            'next_link' => '»',
            'prev_link' => '«',
            'prev_tag_open' => '<li>',
            'prev_tag_close' => '</li>',
            'first_tag_open' => '<li>',
            'first_tag_close' => '</li>',
            'last_tag_open' => '<li>',
            'last_tag_close' => '</li>',
            'last_link' => 'Last',
            'first_link' => 'First',
            'next_tag_open' => '<li>',
            'next_tag_close' => '</li>',
            'previous_tag_open' => '<li>',
            'previous_tag_close' => '</li>'
        );

        //page number.
        $page = $this->uri->segment(4);
        //Get offset
        $offset = ((!empty($page) ? $this->uri->segment(4) : 1) - 1 ) * $aPager['per_page'];
        $this->_list = $this->product->gets($offset, $aPager['per_page'], $squery);
        $this->product->set_pager($aPager);
        $this->_adata['pages'] = $this->product->get_pager('search/index/index/', 4);
    }

}

?>